package com.example.dao;

import com.example.bean.LearningHistory;
import com.example.bean.UserBean;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;



import java.util.Date;
import java.util.List;

@Service(value = "LearningDaoImpl")
public class LearningDaoImpl implements LearningDao{

    @Autowired
    private JdbcTemplate jdbcTemplate; //jdbc连接工具类

    @Override
    public Integer getUserIdByUserName(String username) {
        String SQL = "SELECT id FROM user WHERE username = ?";
        return jdbcTemplate.queryForObject(SQL, new Object[]{username}, Integer.class);
    }



    @Override
    public Long findCourseIdByContent(String content) {
        String sql = "SELECT id FROM course WHERE content = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{content}, Long.class);
    }



    @Override
    public List<LearningHistory> findLearningHistoryByUserId(Integer userId) {
        System.out.println("Looking up learning history for user with id: "+userId.toString());
        String sql = "SELECT * FROM view_learning_history_details WHERE userid = ?";
        return jdbcTemplate.query(sql, new Object[]{userId}, new BeanPropertyRowMapper<>(LearningHistory.class));
    }

    @Override
    public List<LearningHistory> findPracticeHistoryByUserId(Integer userId) {
        System.out.println("Looking up Practice history for user with id: "+userId.toString());
        String sql = "SELECT * FROM view_practice_history_details WHERE userid = ?";
        return jdbcTemplate.query(sql, new Object[]{userId}, new BeanPropertyRowMapper<>(LearningHistory.class));
    }



    @Override
    public void insertLearningRecord(Integer userId, Long courseId) {

        System.out.println("Inserting/Updating history for user " + userId.toString() + " in course " + courseId.toString());
        // 检查学习记录是否存在
        String checkSql = "SELECT COUNT(*) FROM learning_history WHERE userid = ? AND courseid = ?";
        int count = jdbcTemplate.queryForObject(checkSql, new Object[]{userId, courseId}, Integer.class);

        if (count > 0) {
            // 存在记录，更新学习时间
            String updateSql = "UPDATE learning_history SET history = ? WHERE userid = ? AND courseid = ?";
            jdbcTemplate.update(updateSql, new Date(), userId, courseId);
        } else {
            // 不存在记录，插入新记录
            String insertSql = "INSERT INTO learning_history (userid, courseid, history) VALUES (?, ?, ?)";
            jdbcTemplate.update(insertSql, userId, courseId, new Date());
        }
    }
    @Override
    public Integer getExpByCourseId(Long courseId) {
        String sql = "SELECT exp FROM course WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{courseId}, Integer.class);
    }

    public boolean levelUpForCourse(String username, Integer courseExp) {
        String sql = "UPDATE user SET exp = exp + ? WHERE username = ?";
        Object[] params = new Object[]{courseExp, username};
        System.out.println("User experience level up: "+courseExp.toString());
        // 执行更新操作，如果影响的行数大于0，则返回true
        return jdbcTemplate.update(sql, params) > 0;
    }
    @Override
    public Integer countAlgorithmNo(Integer userId) {
        String sql = "SELECT COUNT(DISTINCT algorithm_name) FROM view_learning_history_details WHERE userid = ?";
        Integer count = jdbcTemplate.queryForObject(sql, new Object[]{userId}, Integer.class);
        return count != null ? count : 0; // 如果查询结果为null，则返回0
    }

    @Override
    public Integer countCourseNo(Integer userId) {
        String sql = "SELECT COUNT(DISTINCT course_name) FROM view_learning_history_details WHERE userid = ?";
        Integer count = jdbcTemplate.queryForObject(sql, new Object[]{userId}, Integer.class);
        return count != null ? count : 0; // 如果查询结果为null，则返回0
    }


}


